package edu.ynu.MyProject.entity;
import edu.ynu.MyProject.common.entity.LogicEntity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import javax.persistence.Column;
import javax.persistence.Entity;
import java.time.LocalDateTime;


@Getter
@Setter
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Token extends LogicEntity {
    @Column(unique = true, nullable = true, length = 2048)
    private String token;

    @Column
    private Long expires_in;

    @Column
    private String token_type;

    @Column
    private String scope;

    // 检查令牌是否过期
    public boolean isExpired() {
        // 如果创建时间为空或过期时长无效（负数），认为令牌已经过期
        if (createTime == null || expires_in < 0) {
            return true;
        }

        if (this.scope.equals("fbox")) {
            LocalDateTime expiryTime = createTime.plusSeconds(expires_in);
            return LocalDateTime.now().isAfter(expiryTime);
        } else if (this.scope.equals("camera")) {
            // 获取当前系统时间的毫秒数
            long currentMillis = System.currentTimeMillis();
            // 判断当前时间是否超过 expireTime
            return currentMillis > expires_in;
        }
        // 没有匹配到有效的scope，返回 false
        return false;
    }
}
